$(function () {
    vm.loanRecom();
    localStorage.setItem('username',getQueryString('username'));
    localStorage.setItem('phone',getQueryString('phone'));
    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if(scrollTop + windowHeight == scrollHeight) {
            vm.loadMore();
        }
    });
});

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}

var vm = new Vue({
    el:"#app",
    data:{
        loans:[],
        loadTips:"加载更多..",
        loading:false,
        page:1,
        recomData:{},
        limit:6
    },
    methods:{
        loanRecom:function () {
            $.ajax({
                url:"/thmlfApi/loan/recom",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 200){
                        if (result.result == null){
                            document.getElementById('top-loan').style.display = 'none';
                        }else
                            vm.recomData = result.result;
                    }else
                        alert(result.message);
                },
                complete:function (s) {
                    vm.loadDatas();
                },
                error:function (e) {
                    console.log("error",e);
                    alert("数据请求失败");
                }
            })
        },
        loadDatas:function () {
            $.ajax({
                url:"/thmlfApi/loan/list",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 200){
                        vm.loans = result.result;
                    }else
                        alert(result.message);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("数据请求失败");
                }
            })
        },
        loadMore:function () {
            vm.loading = true;
            vm.page = vm.page+1;
            $.ajax({
                url:"/thmlfApi/loan/list?page="+vm.page+"&limit="+vm.limit,
                dataType:"JSON",
                success:function (result) {
                    vm.loading = false;
                    if (result.code == 200){
                        if (result.result.length != 0)
                            for (var i = 0;i<result.result.length;i++)
                                vm.loans.push(result.result[i]);
                        else {
                            vm.page = vm.page - 1;
                            vm.loadTips = "没有更多数据了";
                        }
                    }else
                        alert(result.message);
                },
                error:function (e) {
                    vm.loading = false;
                    console.log("error",e);
                    alert("请求失败");
                }
            });
        },
        checkUrl:function (str) {
            var RegUrl = new RegExp();
            RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");//jihua.cnblogs.com
            if (!RegUrl.test(str)) {
                return false;
            }
            return true;
        },
        info:function (id) {
            window.location.href = "loanInfo.html?loanId="+id;
        },
        open:function (url,id) {
            var username = localStorage.getItem('username');
            var phone = localStorage.getItem('phone');
            if (username == null || username.length<2 || phone == null || phone.length != 11){
                alert("环境异常，请重试");
                return;
            }
            $.ajax({
                url:'/thmlfApi/loan/record',
                type:'POST',
                data:{'recordUsername':username,'recordPhone':phone,'typeId':id},
                success:function (result) {
                    if (result.code == 200){
                        if (vm.checkUrl(url)){
                            window.location.href = url;
                        }else
                            window.location.href = "tel:"+url;
                    }
                    else
                        alert(result.message);
                },
                error:function (e) {
                    console.error("error",e);
                    alert("请求失败");
                }
            })
        }
    }
});